class ManifestModel {
    constructor(apiModel, dataCollectionModel) {
        this.apiModel = apiModel;
        this.dataCollectionModel = dataCollectionModel;
    }

    generateManifest(appName) {
        const selectedAPIs = this.apiModel.getSelectedAPIs();
        const selectedDataTypes = this.dataCollectionModel.getSelectedTypes();

        return this.generatePlistContent(appName, selectedAPIs, selectedDataTypes);
    }

    generatePlistContent(appName, selectedAPIs, selectedDataTypes) {
        let content = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSPrivacyAccessedAPITypes</key>
    <array>`;

        // 添加 API 类别
        selectedAPIs.forEach(api => {
            if (api.reasons.length > 0) {
                content += `
        <dict>
            <key>NSPrivacyAccessedAPIType</key>
            <string>${api.key}</string>
            <key>NSPrivacyAccessedAPITypeReasons</key>
            <array>`;
                
                api.reasons.forEach(reason => {
                    content += `
                <string>${reason}</string>`;
                });

                content += `
            </array>
        </dict>`;
            }
        });

        content += `
    </array>

    <key>NSPrivacyCollectedDataTypes</key>
    <array>`;

        // 添加数据收集类型
        selectedDataTypes.forEach(dataType => {
            content += `
        <dict>
            <key>NSPrivacyCollectedDataType</key>
            <string>${dataType.typeKey}</string>`;

            // 添加数据使用目标
            dataType.targets.forEach(target => {
                content += `
            <key>${target}</key>
            <true/>`;
            });

            // 添加收集目的
            if (dataType.purposes.length > 0) {
                content += `
            <key>NSPrivacyCollectedDataTypePurposes</key>
            <array>`;
                
                dataType.purposes.forEach(purpose => {
                    content += `
                <string>${purpose}</string>`;
                });

                content += `
            </array>`;
            }

            content += `
        </dict>`;
        });

        content += `
    </array>
</dict>
</plist>`;

        return content;
    }
}

export default ManifestModel; 